# coding:utf-8

import time
import asyncio


async def task_1(x):
    print("开始运行IO任务1.。")
    # yield from 替换为await
    # await asyncio.sleep(1)
    print("IO task 1 had done, used time 1s")
    return x


async def task_2(x):
    print("开始运行IO任务2.。")
    # yield from 替换为await
    await task_1(x)
    print("run code :{}".format(x))
    print("IO task 2 had done, used time 1s")
    return task_2.__name__


async def main():
    # tasks = [task_1(), task_2()]
    tasks = [task_2(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']]
    done, depending = await asyncio.wait(tasks)
    for r in done:
        print('协程无序返回值：' + r.result())


if __name__ == '__main__':
    start = time.time()
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(main())
    finally:
        loop.close()
    print("all task cost time:%.5f" % float(time.time() - start))
